package miblab.school.lists;

import java.io.File;

import miblab.school.R;
import miblab.school.ScreenSchoolActivity;
import miblab.school.SystemParameters;
import miblab.school.views.ViewResults;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class FileList extends Activity{

	public String filenamelist="";
	private int textid;
	private File[] list;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.filelist);

		File mfile = null;
		try {
			mfile = new File(SystemParameters.path+"/School");
			list=mfile.listFiles();
		} catch (Exception e) {
			e.printStackTrace();

		}

		ScrollView sv = new ScrollView(this);
		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);
		sv.addView(ll);




		try {
			for(int i=0;i<mfile.listFiles().length;i++)
			{

				String FN=""+list[i];

				if((FN.contains("Hospital.txt"))||(FN.contains("Home.txt"))||(FN.contains("School.txt"))||(FN.contains("Password.txt"))||(FN.contains("Username.txt")))
				{
				}
				else
				{
					Button b = new Button(this);
					b.setText(FN.substring(FN.length()-16,FN.length()-3));
					b.setId(i);
					ll.addView(b);

					b.setOnClickListener(new View.OnClickListener() {
						public void onClick(View view) {

							textid=view.getId();
							filenamelist=""+list[textid];
							System.out.println("Filenamelist is: " + filenamelist);

							Intent intent = new Intent(FileList.this,ViewResults.class);
							intent.putExtra("EXTRA_FILENAME",filenamelist);
							startActivity(intent);

						}});

				}	
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.setContentView(sv);    

	}

	public String getfn()
	{
		return filenamelist;
	}

	public void setfn(String FN)
	{
		this.filenamelist=FN;
	}
	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(FileList.this,ScreenSchoolActivity.class);
		startActivity(intent);
	}





}
